﻿using System;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.WindowsMobile.PocketOutlook.MessageInterception;
namespace SMSRulez
{
   sealed class Program
    {
    
        private MessageInterceptor mi;
        static string location = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);

        static void Main(string[] args)
        {
            try
            {
                IntPtr hOldCursor = Win32.SetCursor(IntPtr.Zero);
            }
            catch { }
           Program pg = new Program();
           pg.RegisterApplication();
           //pg.KillMe();
        }   
        #region TObeFixed
        private void RegisterApplication()
        {
            bool appRegistered = MessageInterceptor.IsApplicationLauncherEnabled("SMSRules");
            if (appRegistered == true)
            {
                mi = new MessageInterceptor("SMSRules", false); //Load the existing settings
            }
            else
            {
                mi = new MessageInterceptor(InterceptionAction.Notify, false);
                //mi.MessageCondition = new MessageCondition(MessageProperty.MessageClass, MessagePropertyComparisonType.Equal, "SMS", false);
                string appPath = Assembly.GetExecutingAssembly().GetName().CodeBase;
                mi.EnableApplicationLauncher("SMSRules", appPath);
                appRegistered = true;
            }
            mi.MessageReceived += SmsInterceptor_MessageReceived_OnThread;
            
        }

        private void SmsInterceptor_MessageReceived_OnThread(object sender, MessageInterceptorEventArgs e)
        {
            //AppDomain.CurrentDomain.ExecuteAssembly(location + @"\" + "SMSRules.exe auto");
            try
            {
                //ProcessCE.FindAndKill(@"\Windows\tmail.exe");
                //ProcessCE.FindAndKill(@"\Windows\poutlook.exe");


                Process pc = new Process();
                pc.StartInfo.FileName = location + @"\" + "SMSRules.exe";
                pc.StartInfo.Arguments = "auto";
                pc.Start();

                //System.Threading.Thread th = new System.Threading.Thread(ThreadRun);
                //th.IsBackground = true;
                //th.Start();
            
                
            }
            catch { }
            
            
        }

        private void ThreadRun()
        {
            Process pc = new Process();
            pc.StartInfo.FileName = location + @"\" + "SMSRules.exe";
            pc.StartInfo.Arguments = "auto";
            pc.Start();
            
        }
        private void KillMe()
        {
            try
            {
                Process pc = Process.GetCurrentProcess();
                // pc.Close();
                if (!pc.HasExited)
                    pc.Kill();
            }
            catch { }
        }
        #endregion
    }
}
